﻿namespace Frameworks
{
    using System;
    using System.IO;
    using UnityEngine;

    public class FileUtility
    {
        public static void CreatFile(string path)
        {
            if (!File.Exists(path)) //如果不存在则创建
            {
                using (FileStream fs = File.Create(path))
                {
                    fs.Flush();
                    fs.Close();
                    fs.Dispose();
                }
            }
        }

        public static void WriteLineTxt(string path, string msg, bool append = true)
        {
            CreatFile(path);
            using (StreamWriter sw = new StreamWriter(path, append))
            {
                sw.WriteLine(msg);
                sw.Flush();
                sw.Close();
                sw.Dispose();
            }
        }

        public static string ReadAllTxt(string path)
        {
            string msg = "";
            using (StreamReader sw = new StreamReader(path))
            {
                msg = sw.ReadToEnd(); ;
                sw.Close();
                sw.Dispose();
            }
            return msg;
        }

        public static byte[] ReadBytes(string fileFullName)
        {
            byte[] bytes;
            using (FileStream fs = new FileStream(fileFullName, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    bytes = br.ReadBytes((int)fs.Length);
                }
                Debug.Log(bytes.Length);
                Debug.Log((char)(bytes[0]));
            }
            return bytes;
        }

        public static void SaveBytes(string fileFullName, byte[] data)
        {
            if (File.Exists(fileFullName))
                File.Delete(fileFullName);
            else if (!Directory.Exists(GetDirectoryName(fileFullName)))  //判断目录是否存在
                Directory.CreateDirectory(GetDirectoryName(fileFullName));

            using (FileStream fs = new FileStream(fileFullName, FileMode.Create))
            {
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    bw.Write(data); //写入
                    bw.Flush(); //清空缓冲区
                    bw.Close(); //关闭流
                }
                fs.Close();
            }

        }
        /// <summary>
        /// 获取文件的目录
        /// </summary>
        /// <param name="fileFullName">文件全路径</param>
        /// <returns>返回目录全路径</returns>
        public static string GetDirectoryName(string fileFullName)
        {
            return fileFullName.Substring(0, fileFullName.LastIndexOf('/'));
        }

        public static void DeleteFileOrDirectory(string path)
        {
            if (!File.Exists(path) && !Directory.Exists(path)) return;
            FileAttributes attr = File.GetAttributes(path);
            if (attr == FileAttributes.Directory)
            {
                Directory.Delete(path, true);
            }
            else
            {
                File.Delete(path);
            }
        }

        public static string ChanageToDirectoryFilePath(string path)
        {
            if (path == "" || path == string.Empty) return path;
            path.Replace("\\", "/");
            int index = path.LastIndexOf("/");
            if (index != path.Length - 1) path = path + "/";
            return path;
        }

        /// <summary>
        /// 删除掉空文件夹
        /// 所有没有子“文件系统”的都将被删除
        /// </summary>
        /// <param name="storagepath"></param>
        public static void DeleteEmptyDirectory(String storagepath)
        {
            DirectoryInfo dir = new DirectoryInfo(storagepath);
            DirectoryInfo[] subdirs = dir.GetDirectories("*.*", SearchOption.AllDirectories);
            foreach (DirectoryInfo subdir in subdirs)
            {
                FileSystemInfo[] subFiles = subdir.GetFileSystemInfos();
                if (subFiles.Length == 0)
                {
                    subdir.Delete();
                }
            }
        }
        /// <summary>
        /// 根据地址，返回单一的文件名
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetSimpleName(string path)
        {
            path.Replace("\\", "/");
            int index = path.LastIndexOf("/");
            if (index == path.Length - 1) path = path.Substring(0, path.Length - 1);
            index = path.LastIndexOf("/");
            path = path.Substring(index + 1);
            index = path.LastIndexOf(".");
            if (index != -1) path = path.Substring(0, index);
            return path;
        }
        /// <summary>
        /// 根据完整路径返回相对路径
        /// </summary>
        /// <param name="path"></param>
        /// <param name="splitFlag"></param>
        /// <returns></returns>
        public static string GetRelativePath(string path, string splitFlag)
        {
            string bundlPath = path;
            bundlPath.Replace("\\", "/");
            int index = bundlPath.IndexOf(splitFlag);
            bundlPath = bundlPath.Substring(index + splitFlag.Length + 1);
            return bundlPath;
        }
        /// <summary>
        /// 递归返回所有文件，包括子目录
        /// 输入为相对路径返回相对路径，输入绝对路径返回绝对路径
        /// </summary>
        /// <param name="path"></param>
        /// <param name="searchPattern"></param>
        /// <returns></returns>
        public static string[] GetAllFiles(string path, string searchPattern = "*")
        {
            return Directory.GetFiles(path, searchPattern, SearchOption.AllDirectories);
        }
        /// <summary>
        /// 递归返回所有目录，包括子目录
        /// 输入为相对路径返回相对路径，输入绝对路径返回绝对路径
        /// </summary>
        /// <param name="path"></param>
        /// <param name="searchPattern"></param>
        /// <returns></returns>
        public static string[] GetAllDirectories(string path, string searchPattern = "*")
        {
            return Directory.GetDirectories(path, searchPattern, SearchOption.AllDirectories);
        }
    }

}

